/**

 @Name：layuiAdmin 用户管理 管理员管理 角色管理
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL
    
 */


layui.define(['table', 'form', "tree"], function(exports) {
	var $ = layui.$,
		setter = layui.setter,
		admin = layui.admin,
		view = layui.view,
		table = layui.table,
		tree = layui.tree,
		form = layui.form;

	table.render({
		elem: '#LAY-result-back-manage',
		method: "post",
		url: setter.api_source + 'result/list',
		toolbar: true,
		parseData: function(res) { //res 即为原始返回的数据
			return {
				"code": res.code, //解析接口状态
				"msg": res.message, //解析提示文本
				"count": res.data.total, //解析数据长度
				"data": res.data.rows //解析数据列表
			};
		},
		cols: [
			[
				{
					align: "center",
					rowspan: 2,
					type: 'checkbox',
					fixed: 'left'
				}
				,
				{
					align: "center",
					rowspan: 2,
					title: '姓名',
					field: 'name',
					sort: true,
				},
				{
					align: "center",
					rowspan: 2,
					title: '部门',
					field: 'deptName',
					sort: true,
				},
				{
					align: "center",
					title: '学生评价',
					field: 'option',
					colspan: 3
				},
				{
					align: "center",
					colspan: 3,
					title: '自评'
				},
				{
					align: "center",
					title: '互评',
					field: 'option',
					colspan: 3
				},
				{
					align: "center",
					colspan: 3,
					title: '教研室评价'
				},
				{
					align: "center",
					colspan: 3,
					title: '系部评价'
				},
				{
					align: "center",
					rowspan: 2,
					title: '总分',
					field: 'score',
					sort: true,
				}
			],
			[
				{
					align: "center",
					title: '实评人数',
					templet: '<div>{{d.student.reality}}</div>'
				}, {
					align: "center",
					title: '应评人数',
					templet: '<div>{{d.student.should}}</div>'
				}, {
					align: "center",
					title: '得分',
					templet: '<div>{{d.student.score}}</div>',
					sort: true,
					
				},
				
				{
					align: "center",
					title: '实评人数',
					templet: '<div>{{d.teacherSelf.reality}}</div>'
				}, {
					align: "center",
					title: '应评人数',
					templet: '<div>{{d.teacherSelf.should}}</div>'
				}, {
					align: "center",
					title: '得分',
					templet: '<div>{{d.teacherSelf.score}}</div>',
					sort: true,
				},
				
				{
					align: "center",
					title: '实评人数',
					templet: '<div>{{d.teacherEach.reality}}</div>'
				}, {
					align: "center",
					title: '应评人数',
					templet: '<div>{{d.teacherEach.should}}</div>'
				}, {
					align: "center",
					title: '得分',
					templet: '<div>{{d.teacherEach.score}}</div>',
					sort: true,
				},
				
				{
					align: "center",
					title: '实评人数',
					templet: '<div>{{d.staff.reality}}</div>'
				}, {
					align: "center",
					title: '应评人数',
					templet: '<div>{{d.staff.should}}</div>'
				}, {
					align: "center",
					title: '得分',
					templet: '<div>{{d.staff.score}}</div>',
					sort: true,
				},
				
				{
					align: "center",
					title: '实评人数',
					templet: '<div>{{d.department.reality}}</div>'
				}, {
					align: "center",
					title: '应评人数',
					templet: '<div>{{d.department.should}}</div>'
				}, {
					align: "center",
					title: '得分',
					templet: '<div>{{d.department.score}}</div>',
					sort: true,
				},
			]
			// ,
			// [
			// 	{
			// 		field: 'userAccount',
			// 		title: '应评人数'
			// 	}, {
			// 		field: 'studentNo',
			// 		title: '应评人数'
			// 	}, {
			// 		field: 'name',
			// 		title: '得分'
			// 	}
			// ]
			// [{
			// 	type: 'checkbox',
			// 	fixed: 'left',
			// }, {
			// 	field: 'id',
			// 	width: 80,
			// 	title: 'ID',
			// 	sort: true,
			// }, {
			// 	field: 'userAccount',
			// 	title: '账号'
			// }, {
			// 	field: 'studentNo',
			// 	title: '学号'
			// }, {
			// 	field: 'name',
			// 	title: '昵称'
			// }, {
			// 	field: 'clazzName',
			// 	title: '班级',
			// 	align: "center",
			// 	templet: '<div><button class="layui-btn layui-btn-xs">{{d.clazzName}}</button></div>'
			// }, {
			// 	title: '操作',
			// 	width: 250,
			// 	align: 'center',
			// 	fixed: 'right',
			// 	toolbar: '#table-result-admin'
			// }]
		],
		text: '对不起，加载出现异常！',
		page: true,
		limit: 10,
		limits: [5, 10, 15],

	});

	// 初始化
	var clazzlist = [];
	var userlist = [];
	var addData = {};

	function init() {
		/**
		 * 请求班级option列表
		 * */
		admin.req({
			type: "post",
			url: setter.api_source + 'clazz/optionlist',
			beforeSend: function(XMLHttpRequest) {
				//ShowLoading();
			},
			success: function(data, textStatus) {
				clazzlist = data.data.options
				addData["clazzlist"] = clazzlist;
			},
			complete: function(XMLHttpRequest, textStatus) {
				//HideLoading();
			},
			error: function() {
				//请求出错处理
			}
		});
		/**
		 * 请求用户option列表
		 * */
		admin.req({
			type: "post",
			url: setter.api_source + 'sysuser/optionlist',
			beforeSend: function(XMLHttpRequest) {
				//ShowLoading();
			},
			success: function(data, textStatus) {
				userlist = data.data.options
				addData["userlist"] = userlist;

			},
			complete: function(XMLHttpRequest, textStatus) {
				//HideLoading();
			},
			error: function() {
				//请求出错处理
			}
		});
	}
	init();

	//监听工具条
	table.on('tool(LAY-result-back-manage)', function(obj) {
		var data = obj.data;
		var tempdata = obj.data;
		tempdata["clazzlist"] = clazzlist;
		tempdata["userlist"] = userlist;
		if (obj.event === 'del') {
			layer.prompt({
				formType: 1,
				title: '敏感操作，请验证口令'
			}, function(value, index) {
				layer.close(index);
				layer.confirm('确定删除此学生？', function(index) {

					admin.req({
						type: "get",
						url: setter.api_source + 'result/delete/' + obj.data.id,
						beforeSend: function(XMLHttpRequest) {
							//ShowLoading();
						},
						success: function(data, textStatus) {
							obj.del();
							layer.close(index);
						},
						complete: function(XMLHttpRequest, textStatus) {
							//HideLoading();
						},
						error: function() {
							//请求出错处理
						}
					});
				});
			});
		} else if (obj.event === 'edit') {
			admin.popup({
				title: '编辑学生',
				area: ['420px', '450px'],
				id: 'LAY-popup-result-add',
				success: function(layero, index) {

					// 指定要渲染的模板html
					view(this.id).render('user/tes/result/resulteditform', tempdata).done(function() {
						// 指定渲染的fiter
						form.render(null, 'result-edit-form');
						//监听提交
						form.on('submit(LAY-result-back-submit)', function(data) {
							var field = data.field; //获取提交的字段

							admin.req({
								type: "post",
								data: field,
								url: setter.api_source + 'result/update',
								beforeSend: function(XMLHttpRequest) {
									//ShowLoading();
								},
								success: function(data, textStatus) {
									init();
									layui.table.reload('LAY-result-back-manage'); //重载表格
									layer.close(index); //执行关闭 
								},
								complete: function(XMLHttpRequest, textStatus) {
									//HideLoading();
								},
								error: function() {
									//请求出错处理
								}
							});
						});
					});
				}
			});
		}
	});

	exports('result', {})
});
